import { defineStore } from 'pinia'
import { createCarousel, updateCarousel, deleteCarousel, getCarouselDetail, pageCarousels, listEnabledCarousels, setCarouselStatus } from '@/api/carousel'

export const useCarouselStore = defineStore('carousel', {
  state: () => ({
    carousels: [],
    total: 0,
    loading: false,
    enabledCarousels: [],
    carouselDetail: {}
  }),
  
  actions: {
    // 分页获取轮播图列表
    async pageCarousels(params) {
      this.loading = true
      try {
        const response = await pageCarousels(params)
        const { data } = response
        this.carousels = data.records || []
        this.total = data.total || 0
        this.loading = false
        return Promise.resolve(data)
      } catch (error) {
        this.loading = false
        return Promise.reject(error)
      }
    },
    
    // 获取已启用的轮播图
    async listEnabledCarousels() {
      try {
        const response = await listEnabledCarousels()
        const { data } = response
        this.enabledCarousels = data
        return Promise.resolve(data)
      } catch (error) {
        return Promise.reject(error)
      }
    },
    
    // 获取轮播图详情
    async getCarouselDetail(id) {
      try {
        const response = await getCarouselDetail(id)
        const { data } = response
        this.carouselDetail = data
        return Promise.resolve(data)
      } catch (error) {
        return Promise.reject(error)
      }
    },
    
    // 创建轮播图
    async createCarousel(carouselData) {
      try {
        const response = await createCarousel(carouselData)
        return Promise.resolve(response)
      } catch (error) {
        return Promise.reject(error)
      }
    },
    
    // 更新轮播图
    async updateCarousel(carouselData) {
      try {
        const response = await updateCarousel(carouselData)
        return Promise.resolve(response)
      } catch (error) {
        return Promise.reject(error)
      }
    },
    
    // 删除轮播图
    async deleteCarousel(id) {
      try {
        const response = await deleteCarousel(id)
        return Promise.resolve(response)
      } catch (error) {
        return Promise.reject(error)
      }
    },
    
    // 设置轮播图状态
    async setCarouselStatus(id, status) {
      try {
        const response = await setCarouselStatus(id, status)
        return Promise.resolve(response)
      } catch (error) {
        return Promise.reject(error)
      }
    }
  }
}) 